<template>
  <div class="page">
    <!-- <h3>登录</h3> -->
    <div class="header"><my-back></my-back></div>
    <div class="login">
      <section>
        <div class="top">
          <img src="../../static/images/login/logo.png" alt="" />
          <p>登录</p>
        </div>
        <div class="form">
          <div>
            <i></i>
            <input
              v-model="formData.phone"
              type="text"
              placeholder="请输入您的账号/邮箱/手机号"
            />
          </div>

          <div>
            <i class="pwd"></i>
            <input
              v-model="formData.password"
              type="password"
              placeholder="请输入密码"
            />
          </div>

          <div class="opt">
            <div class="l"><input type="checkbox" /> 记住账号</div>
            <div class="r">
              <router-link to="/findpwd">忘记密码?</router-link>
            </div>
          </div>
          <div>
            <button :class="formData.phone ? 'active' : ''" @click="loginFn">
              登录
            </button>
          </div>
        </div>
        <router-link to="/register">去注册</router-link>
      </section>
    </div>

    <!-- 正在加载 -->
    <div class="loading" v-if="isLoading">
      <ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </div>
  </div>
</template>


<style scoped lang="less">
@import "@/static/less/variable.less";
.page {
  padding: @pad;
}
.login {
  .top {
    text-align: center;
    margin-top: 50px;
    img {
      width: 80px;
    }
    p {
      margin-top: 20px;
    }
  }
  .form {
    margin-top: 40px;
    width: 100%;
    > div {
      width: 100%;
      margin-bottom: 20px;
      position: relative;
      height: 56px;
      input[type="text"],
      input[type="password"] {
        width: 100%;
        height: 56px;
        padding-left: 44px;
        font-size: 16px;
        box-sizing: border-box;
        border-radius: 10px;
      }
      i {
        position: absolute;
        top: 15px;
        left: 10px;
        width: 28px;
        height: 26px;
        background-image: url(../../static/images/login/message.png);
        background-size: 100% 100%;
      }
      i.pwd {
        background-image: url(../../static/images/login/lock.png);
      }

      button {
        height: 56px;
        width: 100%;
        background-color: @bgColor;
        color: #fff;
        border-radius: 10px;
        border-color: @bgColor;
        opacity: 0.6;
      }
      .active {
        opacity: 1;
      }
    }

    .opt {
      width: 100%;
      height: 30px;
      display: flex;
      // background-color: red;
      justify-content: space-between;
      align-items: center;
      input[type="checkbox"] {
        width: 20px;
        height: 20px;
        vertical-align: middle;
      }
      a {
        color: #000;
      }
    }
  }
}
h3 {
  font-weight: normal;
}

// 正在加载
.loading {
  position: fixed;
  z-index: 1000;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  ul {
    display: flex;
    align-items: center;
    li {
      height: 50px;
      width: 2px;
      background-color: red;
      margin: 0 6px;
      &:nth-child(1) {
        animation: myAnimate 0.5s 0.15s infinite;
      }
      &:nth-child(2) {
        animation: myAnimate 0.5s 0.05s infinite;
      }
      &:nth-child(3) {
        animation: myAnimate 0.5s 0.1s infinite;
      }
      &:nth-child(4) {
        animation: myAnimate 0.5s 0.2s infinite;
      }
      &:nth-child(5) {
        animation: myAnimate 0.5s 0.08s infinite;
      }
    }
  }
}

@keyframes myAnimate {
  0% {
    height: 30px;
  }
  50% {
    height: 60px;
  }
  100% {
    height: 30px;
  }
}
</style>

<script>
import MyBack from "@/components/MyBack.vue";

// 导入网络接口
import { sendCaptcha, login } from "@/api/index";

export default {
  components: {
    MyBack,
  },
  data() {
    return {
      // 手机用户信息的对象
      formData: {
        phone: "",
        password: "",
      },
      // 控制显示正在加载的效果
      isLoading: false,
    };
  },
  methods: {
    // 登录
    loginFn() {
      let { phone, password } = this.formData;
      if (phone.length == 0 || password.length == 0) {
        alert("输入框不能为空"); //以后可以自定义弹窗
        return;
      }

      // 显示正在加载
      this.isLoading = true;

      // 现在本地储存空间取值查询 (以后通过接口从数据库查询)
      let arr_str = localStorage.getItem("_ARR");
      console.log({ arr_str });

      if (arr_str == null) {
        alert("该账号不存在");
        this.isLoading = false;
        return;
      }

      let arr = JSON.parse(arr_str);
      console.log("所有用户信息", arr);
      let success = false;
      let TOKEN = "";
      let nickName = "";
      arr.forEach((user) => {
        if (phone == user["phone"]) {
          if (password == user["password"]) {
              success = true;
              TOKEN = user["id"]
              nickName = user["phone"]
              localStorage.setItem("_USER", JSON.stringify(user))
          } 
         } 
      });

      // 判断是否存在这个账号
      if(success){
        //登录成功
          // alert("登录成功");
          // 登录成功,做一个假的token(用户的临时身份)
          let STARTTIME = new Date().getTime();
          localStorage.setItem("_TOKEN", TOKEN);
          localStorage.setItem("_STARTTIME", STARTTIME);
          localStorage.setItem("NICKNAME", nickName);
          alert('登录成功.')
          // 跳转我的
          setTimeout(() => {
            this.$router.push({
              path: "/my",
            });
          }, 1000);
      }
      else {
         alert('登录失败.')
      }

      setTimeout(() => {
        this.isLoading = false;
      }, 1000);
    },

    // async loginFn2() {
    //   // console.log(this.formData)
    //   let { phone, password } = this.formData;
    //   if (password.length == 0 || phone.length == 0) {
    //     alert("输入框不能为空!");
    //     return false;
    //   }
    //   // 修改密码
    //   await login({
    //     phone,
    //     password,
    //   }).then((content) => {
    //     console.log("请求接口:", content);
    //   });
    // },
  },
};
</script>